﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace HttpFileServer
{
    public class Util
    {

        private readonly static int pic_size = 256;
        private readonly static string prefix = "data:image/jpg;base64,";
        public static long UtcNow => (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
        public static string GetThumbnail(string path)
        {
            Bitmap bm = WindowsThumbnailProvider.GetThumbnail(path, pic_size, pic_size, ThumbnailOptions.None);
            using(var ms=new MemoryStream())
            {
                bm.Save(ms, ImageFormat.Jpeg);
                bm.Dispose();
                return prefix+ Convert.ToBase64String(ms.ToArray());
            }
        }
        /// <summary>
        /// base64 replace'/' to '_'
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Sha1(string data)
        {
            var hash=SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(data));
            return Convert.ToBase64String(hash).Replace('/', '_');
        }
        /// <summary>
        /// base64 replace  '/' to '_'
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Sha256(string data)
        {
            var hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(data));
            return Convert.ToBase64String(hash).Replace('/', '_');
        }
        public static bool FileCopy(string from,string to)
        {
            try
            {
                new FileInfo(to).Directory.Create();
                new FileInfo(from).CopyTo(to);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static string CreateID(int len=8)
        {
            var d = Guid.NewGuid().ToString();
            return Sha256(d).ToLower().Substring(0, len);
        }
        public static bool IsVideo(string fileName)
        {
            fileName = fileName.ToLower();
            return fileName.EndsWith(".mp4") || fileName.EndsWith(".ts") || fileName.EndsWith(".mkv");
        }
        public static string AppGuid
        {
            get{
                var file = "salt.dat";
                var fi = new FileInfo(file);
                if (fi.Exists) return File.ReadAllText(file);
                var createSalt = Sha256(Guid.NewGuid().ToString());
                File.WriteAllText(file, createSalt);
                return createSalt;
            }
            set
            {
                var file = "salt.dat";
                var createSalt = Sha256(Guid.NewGuid().ToString()+value);
                File.WriteAllText(file, createSalt);
            }
        }
        public static string tryGet(List<string> list,string index)
        {
            return int.TryParse(index, out int id) && (id >= 0 && id < list.Count) ? list[id] :null;
        }
    }
}
